﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business.Service;
using Model;

namespace MvcAppWeb.Controllers
{
    public class DCThirdInfoController : BaseDataController
    {
        public ActionResult DCThirdInfo()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询第三方信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string ThirdName, string status)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (ThirdName != "")
            {
                where += " and t1.ThirdName like'%" + ThirdName + "%'";
            }
            if (status != "")
            {
                where += " and t1.status='" + status + "'";
            }
            var retData = new object();
            BDCThirdInfo db = new BDCThirdInfo();
            retData = db.GetDCThirdInfo(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }



        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                DCThirdInfo _BDCThirdInfo = new DCThirdInfo();

                string ThirdId = Request["hiddenTHIRDID"].Trim(); ;

                _BDCThirdInfo.THIRDNAME = Request["stxtThirdName"].ToString();
                _BDCThirdInfo.OPRNO = model.USERCODE.ToString();
                _BDCThirdInfo.STATUS = Request["sstatus"].ToString();
                if (Request["sWareHouseId"].ToString()=="0")
                {
                    return "{success:false,msg:'请选择仓库！'}";
                }
                _BDCThirdInfo.WAREHOUSEID = Convert.ToInt32(Request["sWareHouseId"].ToString());
                BDCThirdInfo db = new BDCThirdInfo();

                if (!string.IsNullOrEmpty(ThirdId))
                {
                    _BDCThirdInfo.THIRDID = int.Parse(ThirdId);
                }
                List<DCThirdInfo> lstDCThirdInfo = db.GetDCThirdInfoByName(_BDCThirdInfo.THIRDNAME).Where(_ => _.THIRDID != _BDCThirdInfo.THIRDID).ToList();
                if (lstDCThirdInfo != null && lstDCThirdInfo.Count > 0)
                {
                    sjson = "{success:false,msg:'此第三方已经存在！'}";
                }
                else
                {
                    int n = db.MergeDCThirdInfo(_BDCThirdInfo);
                    sjson = "{success:true}";
                }
                return sjson;
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }


        /// <summary>
        /// 获取第三方信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDCThirdInfoByComboBox()
        {
            string defaultValue = Request["defaultValue"].ToString();
            var retData = new object();
            BDCThirdInfo db = new BDCThirdInfo();
            retData = db.GetDCThirdInfoByComboBox(defaultValue).ToList();
            return Json(retData);
        }


    }
}
